﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CoolChat
{
    public class ChatUser
    {
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string userID;

        public string UserID
        {
            get { return userID; }
            set { userID = value; }
        }

        private string avatar;

        /// <summary>
        /// 头像地址
        /// </summary>
        public string Avatar
        {
            get { return avatar; }
            set { avatar = value; }
        }
        private string userIP;

        public string UserIP
        {
            get { return userIP; }
            set { userIP = value; }
        }
        private string userName;

        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }
        private string sessionId;

        public string SessionId
        {
            get { return sessionId; }
            set { sessionId = value; }
        }
        private USER_ROLE userRole;

        public USER_ROLE UserRole
        {
            get { return userRole; }
            set {
                if (value != USER_ROLE.SYSUSER)
                {
                    this.sysUserType = -1;
                }
                userRole = value; 
            }
        }

        /// <summary>
        /// 禁言,时间类型,如果设置了该值在该时间内该用户禁止发言,未设置的值为null
        /// </summary>
        private DateTime silenceExpires = DateTime.Parse("2013-01-01 00:00:00");

        /// <summary>
        /// 禁言,时间类型,如果设置了该值在该时间内该用户禁止发言,未设置的值为null
        /// </summary>
        public DateTime SilenceExpires
        {
            get { return silenceExpires; }
            set
            {
                if (value == DateTime.MinValue)
                {
                    silenceExpires = DateTime.Parse("2013-01-01 00:00:00");
                }
                else
                {
                    silenceExpires = value;
                }
            }
        }


        private int sysUserType = -1;

        public int SysUserType
        {
            get { return sysUserType; }
            set {
                //if (this.userRole != USER_ROLE.SYSUSER)
                //{
                //    sysUserType = -1;
                //    return;
                //}
                sysUserType = value; }
        }
        private DateTime addTime = DateTime.Parse("2013-01-01 00:00:00");

        public DateTime AddTime
        {
            get { return addTime; }
            set {
                if (value == DateTime.MinValue)
                {
                    addTime = DateTime.Parse("2013-01-01 00:00:00");
                }
                else
                {
                    addTime = value;
                }
            }
        }
        private DateTime lastLoginTime = DateTime.Parse("2013-01-01 00:00:00");

        public DateTime LastLoginTime
        {
            get { return lastLoginTime; }
            set
            {
                if (value == DateTime.MinValue)
                {
                    lastLoginTime = DateTime.Parse("2013-01-01 00:00:00");
                }
                else
                {
                    lastLoginTime = value;
                }
            }
        }

        private bool isOnline;

        public bool IsOnline
        {
            get { return isOnline; }
            set { isOnline = value; }
        }

        public static ChatUser GetUser(System.Data.DataRow row)
        {
            ChatUser u = new ChatUser();
            object val = Tools.GetValueFromRow(row,"ID");
            u.Id = val == null || val == DBNull.Value ? 0 : (int)val;
            val = Tools.GetValueFromRow(row, "UserID");
            u.UserID = val == null || val == DBNull.Value ? null : (string)val;
            val = Tools.GetValueFromRow(row, "UserName");
            u.UserName = val == null || val == DBNull.Value ? "*" : (string)val;
            val = Tools.GetValueFromRow(row, "UserIP");
            u.UserIP = val == null || val == DBNull.Value ? "*" : (string)val;
            val = Tools.GetValueFromRow(row, "SessionID");
            u.SessionId = val == null || val == DBNull.Value ? null : (string)val;
            val = Tools.GetValueFromRow(row, "UserRole");
            u.UserRole = val == null || val == DBNull.Value ? USER_ROLE.GUEST : (USER_ROLE)val;
            val = Tools.GetValueFromRow(row, "LastLoginTime");
            u.LastLoginTime = val == null || val == DBNull.Value ? DateTime.MinValue : (DateTime)val;
            val = Tools.GetValueFromRow(row, "silenceExpires");
            u.silenceExpires = val == null || val == DBNull.Value ? DateTime.MinValue : (DateTime)val;
            val = Tools.GetValueFromRow(row, "AddTime");
            u.AddTime = val == null || val == DBNull.Value ? DateTime.MinValue : (DateTime)val;
            val = Tools.GetValueFromRow(row, "SysUserType");
            u.SysUserType = val == null || val == DBNull.Value ? 0 : (int)val;
            val = Tools.GetValueFromRow(row, "IsOnline");
            u.IsOnline = val == null || val == DBNull.Value ? false : (bool)val;
            val = Tools.GetValueFromRow(row, "Avatar");
            u.Avatar = val == null || val == DBNull.Value ? null : (string)val;
            return u;
        }
        public override string ToString()
        {
            return this.userName +" " + this.userIP;
        }
    }
}